from django.contrib import admin
from .models import Story, StoryCategory

class StoryCategoryAdmin(admin.ModelAdmin):
    """故事分类管理界面"""
    list_display = ('name', 'description', 'story_count', 'order', 'created_at')
    list_editable = ('order',)
    search_fields = ('name', 'description')
    list_filter = ('created_at',)
    ordering = ('order', 'created_at')

class StoryAdmin(admin.ModelAdmin):
    """故事管理界面"""
    list_display = ('title', 'author', 'get_category_display', 'is_draft', 'views', 'likes', 'created_at')
    list_filter = ('is_draft', 'category', 'created_at')
    search_fields = ('title', 'content', 'author__username', 'keywords')
    readonly_fields = ('views', 'likes', 'created_at', 'updated_at')
    date_hierarchy = 'created_at'
    actions = ['mark_as_draft', 'mark_as_published']
    
    def mark_as_draft(self, request, queryset):
        """将选中的故事标记为草稿"""
        queryset.update(is_draft=True)
    mark_as_draft.short_description = "将选中的故事标记为草稿"
    
    def mark_as_published(self, request, queryset):
        """将选中的故事标记为发布"""
        queryset.update(is_draft=False)
    mark_as_published.short_description = "将选中的故事发布"

admin.site.register(StoryCategory, StoryCategoryAdmin)
admin.site.register(Story, StoryAdmin)
